package com.xhwl.logistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.vehicle.VehcCarRecord;
import com.xhwl.common.pojo.cent.visitor.VisitorInfo;
import com.xhwl.common.utils.StringUtils;
import com.xhwl.logistics.dao.VehcCarRecordDao;
import com.xhwl.logistics.service.IVehcCarRecordOpenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Slf4j
//@Service
public class VehcCarRecordServiceImpl implements IVehcCarRecordOpenService {

    @Autowired
    VehcCarRecordDao vehcCarRecordDao;

    @Override
    public ResultJson<Integer> getCarEntranceStatus(VisitorInfo visitorInfo) {
        log.info("查询车辆入场状态入参:{}",visitorInfo);
        String carNum = visitorInfo.getCarNum();
        Integer projectId = visitorInfo.getProjectId();
        boolean flag = StringUtils.isBlank(carNum) || projectId == null;
        if (flag){
            log.info("入参错误默认未入场");
            return ResultJson.success(1);
        }
        QueryWrapper<VehcCarRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plate_number",carNum).eq("project_id",projectId).orderByDesc("id");
        List<VehcCarRecord> carRecords = vehcCarRecordDao.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(carRecords)){
            return ResultJson.success(1);
        }else {
            VehcCarRecord carRecord = carRecords.get(0);
            Date enterTime = carRecord.getEnterTime();
            Date existTime = carRecord.getExistTime();
            if (enterTime != null && existTime == null){
                return ResultJson.success(2);
            }else if (enterTime != null && existTime != null){
                return ResultJson.success(3);
            }else {
                return ResultJson.success(1);
            }
        }
    }
}
